#!/bin/bash
set -CeEu
set -o pipefail

SPATH="$(readlink -f "$0")"
SDIR="$(dirname "${SPATH}")"; export SDIR;
SCRIPT_NAME="${0##*/}"

# shellcheck source=common.sh
. "${SDIR}/common.sh"

cd "${SDIR}"

main() {
  # CLI setup
  parse_args "$@"

  run_basic_test "install" "mesh_ca" "-o vm -o hub-meshca --enable_gcp_components --enable_registration"; RETVAL=$?;
  trap 'cleanup_lt_cluster "${LT_NAMESPACE}" "${OUTPUT_DIR}" "${REV}"' ERR

  echo "Verifying east-west gateway is installed and running..."
  kubectl wait --for=condition=available --timeout=600s \
        deployment/istio-eastwestgateway -n istio-system

  echo "Verifying cluster is registered..."
  if ! is_cluster_registered; then
    fatal "Cluster is not registered to an environ."
  fi

  echo "Verifying Google Identity Provider is registered..."
  local RETVAL; RETVAL=0;
  kubectl get identityprovider \
    --request-timeout='20s' \
    google 1>/dev/null 2>/dev/null || RETVAL=$?
  if [[ "${RETVAL}" -ne 0 ]]; then
    fatal "GCE Identity Provider cannot be found."
  fi

  echo "Verifying service mesh feature is enabled..."
  if ! is_service_mesh_feature_enabled; then
    fatal "Service mesh feature is not enabled."
  fi

  cleanup_lt_cluster "${LT_NAMESPACE}" "${OUTPUT_DIR}" "${REV}"
  delete_service_mesh_feature
}

main "$@"
